package com.itheima.hmshopping.app.controller;

import com.itheima.hmshopping.app.service.SmsFlashPromotionService;
import com.itheima.hmshopping.entity.SmsFlashPromotion;
import com.itheima.hmshopping.util.HmPage;
import com.itheima.hmshopping.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 秒杀活动
 */
@RestController
@RequestMapping("flash")
public class SmsFlashPromotionController {

    @Autowired
    private SmsFlashPromotionService smsFlashPromotionService;

    /**
     * 添加活动
     * @param smsFlashPromotion
     * @return
     */
    @PostMapping("create")
    public R create(@RequestBody SmsFlashPromotion smsFlashPromotion) {
        smsFlashPromotionService.create(smsFlashPromotion);
        return R.succeed("ok","");
    }

    /**
     * 删除活动
     * @param id
     * @return
     */
    @PostMapping("/delete/{id}")
    public R delete(@PathVariable Integer id) {
        smsFlashPromotionService.delete(id);
        return R.succeed("ok","");
    }

    /**
     * 根据活动名称分页查询
     *
     * @param keyword
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/list")
    public R list(@RequestParam(value = "keyword", required = false) String keyword, @RequestParam(defaultValue = "1") Integer pageNum,
                  @RequestParam(defaultValue = "10") Integer pageSize) {

       HmPage hmPage=smsFlashPromotionService.list(keyword,pageNum,pageSize);
       return R.succeed(hmPage,"ok");

    }

    /**
     * 修改上下线状态
     *
     * @param id
     * @param status
     * @return
     */
    @PostMapping("update/status/{id}")
    public R updateStatus(@PathVariable Integer id,
                          @RequestParam(value = "status", required = false) Integer status) {
        smsFlashPromotionService.updateStatus(id, status);
        return R.succeed("ok","");
    }

    /**
     * 编辑活动
     * @param id
     * @param promotion
     * @return
     */
    @PostMapping("update/{id}")
    public R updateFlashPromotion(@PathVariable Integer id, @RequestBody SmsFlashPromotion promotion) {
        smsFlashPromotionService.update(id, promotion);
        return R.succeed("ok","");
    }

    /**
     * 获取详情
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R getFlashPromotion(@PathVariable Integer id) {
       SmsFlashPromotion smsFlashPromotion= smsFlashPromotionService.getOne(id);
        return R.succeed(smsFlashPromotion,"");
    }



}
